//
//  BankCardPopViewController.m
//  PPDL
//
//  Created by 王洋 on 2019/7/27.
//  Copyright © 2019 王洋. All rights reserved.
//

#import "BankCardPopCell.h"
#import "PPSettingCell.h"

#import "BankCardPopViewController.h"

@interface BankCardPopViewController () <UITableViewDelegate, UITableViewDataSource>

@property (nonatomic ,strong) NSMutableArray <BankCardModel *> *bankCardListArray;
@property (nonatomic ,strong) UITableView *tableView;

@property (nonatomic ,strong) UIViewController *vc;

@property (nonatomic, assign) NSInteger defaultIndex;
@property (nonatomic, assign) CGFloat   defaultHeight;


@property (nonatomic, strong) UIView *headerView;

@end

@implementation BankCardPopViewController


- (void)dealloc {
    
    
}

- (void)viewDidLoad {
    [super viewDidLoad];

    [self initPop];
    [self setUpUI];
    
    [self.tableView reloadData];
}

- (void)initPop {
    self.view.backgroundColor = [UIColor whiteColor];
//    CGFloat height = 140;
//    height += self.bankCardListArray.count * 60;
    
    self.defaultHeight = self.bankCardListArray.count * 60 + 50  + 60;
    
    if (self.defaultHeight > TTScreenHeight / 2) {
        self.defaultHeight = TTScreenHeight / 2;
    }
    
    self.contentSizeInPopup = CGSizeMake(TTScreenWidth, self.defaultHeight);
    self.popupController.navigationBarHidden = YES;
    [self.popupController.backgroundView addGestureRecognizer:[[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(backgroundTap)]];
}

- (void)setUpUI {
    [self.view addSubview:self.tableView];
    [self.view addSubview:self.headerView];
    
    [self.headerView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.top.right.mas_equalTo(0);
        make.height.mas_equalTo(50);
    }];
    
    
    [self.tableView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.bottom.mas_equalTo(0);
        make.top.mas_equalTo(50);
    }];

}

-(void)closeBlockView {
    [self backgroundTap];
}

- (void)backgroundTap  {
    [self.popupController dismiss];
}

-(UITableView *)tableView {
    if (!_tableView) {
        _tableView = [[UITableView alloc]initWithFrame:self.view.frame style:UITableViewStylePlain];
        _tableView.delegate = self;
        _tableView.dataSource = self;
        _tableView.backgroundColor = [UIColor clearColor];
        _tableView.separatorStyle = UITableViewCellSeparatorStyleNone;

        //银行卡cell
        [_tableView registerNib:[UINib nibWithNibName:@"BankCardPopCell" bundle:nil] forCellReuseIdentifier:@"BankCardPopCellID"];
        
        //添加银行卡的cell
        [_tableView registerNib:[UINib nibWithNibName:@"PPSettingCell" bundle:nil] forCellReuseIdentifier:@"PPSettingCellID"];


    }
    return _tableView;
}

- (UIView *)headerView {
    if (!_headerView) {
        
        _headerView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, TTScreenWidth, 50)];
        _headerView.backgroundColor = [UIColor whiteColor];;
        
        
        UIView *lineView = [UIView new];
        lineView.backgroundColor = [UIColor lightGrayColor];
        [_headerView addSubview:lineView];

        UILabel *title = [[UILabel alloc] init];
        title.text = @"请选择银行卡";
        title.textAlignment = NSTextAlignmentCenter;
        [_headerView addSubview:title];

        UIButton *closeBtn = [UIButton buttonWithType:UIButtonTypeCustom];
//        closeBtn.frame = CGRectMake(TTScreenWidth - 50, 0, 50, 30);
        closeBtn.backgroundColor = [UIColor greenColor];
        [closeBtn setTitle:@"X" forState:UIControlStateNormal];
        [closeBtn setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
        [closeBtn addTarget:self action:@selector(closeBlockView) forControlEvents:UIControlEventTouchUpInside];
        [_headerView addSubview:closeBtn];
        
        [title mas_makeConstraints:^(MASConstraintMaker *make) {
            make.centerX.centerY.mas_equalTo(0);
        }];
        
        [lineView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.right.bottom.mas_equalTo(0);
            make.height.mas_equalTo(0.5);
        }];
        
        [closeBtn mas_makeConstraints:^(MASConstraintMaker *make) {
            make.top.right.bottom.mas_equalTo(0);
            make.width.mas_equalTo(50);
        }];
        
    }
    return _headerView;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return self.bankCardListArray.count + 1;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    
    if (indexPath.row < self.bankCardListArray.count) {
        BankCardPopCell *cell = [tableView dequeueReusableCellWithIdentifier:@"BankCardPopCellID" forIndexPath:indexPath];
        cell.selectionStyle =UITableViewCellSelectionStyleNone;
        
        [cell fillCellWithModel:self.bankCardListArray[indexPath.row]];
        
        cell.refreshItemModel = ^(BankCardModel * _Nonnull model) {
            
        };
        
        return cell;
    } else {
        
        PPSettingCell *cell = [tableView dequeueReusableCellWithIdentifier:@"PPSettingCellID" forIndexPath:indexPath];
        cell.selectionStyle =UITableViewCellSelectionStyleNone;
        
//        [cell fillCellWithModel:self.listArray[indexPath.row]];
        
        return cell;
    }

}

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return 60;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {

    [self closeBlockView];

    if (self.selectIndex) {
        self.selectIndex(indexPath.row);
    }
}

- (instancetype)initDefaultIndex:(NSInteger)index defaultHeight:(CGFloat)height vc:(UIViewController *)vc dataSource:(NSMutableArray <BankCardModel *>*)dataSource {

    self.bankCardListArray = @[].mutableCopy;
    if (self = [super init]) {
        self.vc = vc;
        self.bankCardListArray = dataSource.mutableCopy;
        self.defaultIndex = index;
        self.defaultHeight = height;
    }
    return self;
}











@end
